import wx

class LoginFrame(wx.Frame):
    def __init__(self, parent, title):
        super(LoginFrame, self).__init__(parent, title=title, size=(800, 600))

        # 设置背景图片
        self.background_image = wx.Image("F:\\login.png").ConvertToBitmap()
        self.background = wx.StaticBitmap(self, -1, self.background_image, (0, 0))

        # 创建一个面板
        panel = wx.Panel(self, style=wx.NO_BORDER)  # 去掉面板的边框

        # 创建一个FlexGridSizer，4行2列
        grid_sizer = wx.FlexGridSizer(rows=4, cols=2, vgap=10, hgap=10)

        # 创建用户名输入框
        self.username_label = wx.StaticText(panel, label="用户名:", style=wx.NO_BORDER)
        self.username_text = wx.TextCtrl(panel)
        grid_sizer.Add(self.username_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
        grid_sizer.Add(self.username_text, 0, wx.EXPAND)

        # 创建密码输入框
        self.password_label = wx.StaticText(panel, label="密码:", style=wx.NO_BORDER)
        self.password_text = wx.TextCtrl(panel, style=wx.TE_PASSWORD)
        grid_sizer.Add(self.password_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
        grid_sizer.Add(self.password_text, 0, wx.EXPAND)

        # 创建验证码输入框
        self.captcha_label = wx.StaticText(panel, label="验证码:", style=wx.NO_BORDER)
        self.captcha_text = wx.TextCtrl(panel)
        grid_sizer.Add(self.captcha_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
        grid_sizer.Add(self.captcha_text, 0, wx.EXPAND)

        # 创建登录按钮
        self.login_button = wx.Button(panel, label="登录")
        self.login_button.Bind(wx.EVT_BUTTON, self.on_login)
        grid_sizer.Add((0, 0), 0, wx.EXPAND)  # 空占位符
        grid_sizer.Add(self.login_button, 0, wx.ALIGN_CENTER)

        # 设置布局
        panel.SetSizer(grid_sizer)

        # 将面板放置在窗口的右侧三分之一处
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        main_sizer.AddStretchSpacer(prop=2)  # 左侧占2/3
        main_sizer.Add(panel, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 10)  # 右侧占1/3
        self.SetSizer(main_sizer)

        # 确保背景图片覆盖整个窗口
        # self.background.SetSize(self.GetSize())
        # self.Bind(wx.EVT_SIZE, self.on_size)

    def on_size(self, event):
        self.background.SetSize(self.GetSize())
        event.Skip()

    def on_login(self, event):
        username = self.username_text.GetValue()
        password = self.password_text.GetValue()
        captcha = self.captcha_text.GetValue()

        # 调用登录接口
        if self.login_api(username, password, captcha):
            wx.MessageBox("登录成功!", "信息", wx.OK | wx.ICON_INFORMATION)
        else:
            wx.MessageBox("登录失败!", "错误", wx.OK | wx.ICON_ERROR)

    def login_api(self, username, password, captcha):
        # 这里模拟登录接口调用
        # 实际应用中，你需要替换为实际的API调用
        if username == "admin" and password == "password" and captcha == "1234":
            return True
        else:
            return False

# if __name__ == "__main__":
#     app = wx.App(False)
#     frame = LoginFrame(None, "登录界面")
#     frame.Show()
#     app.MainLoop()


app = wx.App()
window = wx.Frame(parent=None, title="hello Python GUI APP!")
window.Show()
app.MainLoop()

